// packageF/debt/purchaseDetails/purchaseDetails.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    listData: [],
    tabNav:[],
    coupon:false,
    isvip:false,

    //more
    isLoadMore: true,
    page: 1,
    total_page: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const plugin_setting = wx.getStorageSync('yz_basic_info').plugin_setting.debt_shop || {}
    if(options.debtId){
      this.setData({
        debtId:options.debtId
      })
    }
    if(options.coupon){
      this.setData({
        coupon : true
      })
      this.setData({
        tabNav:['全部',`${plugin_setting.common_credit_name?plugin_setting.common_credit_name:"消费账户"}`,`${plugin_setting.frozen_credit_name?plugin_setting.frozen_credit_name:"锁定账户"}`]
      })
      wx.setNavigationBarTitle({
        title: '提货券明细',
      })
    }else if(options.isvip){
      this.setData({
        tabNav:[],
        isvip:true
      })
      wx.setNavigationBarTitle({
        title: '提货券明细',
      })
    } else{
      this.setData({
        tabNav:['全部','采购金','网店收入']
      })
      wx.setNavigationBarTitle({
        title: '采购款明细',
      })
    }
    this.getData();
  },
  init() {
    this.setData({
      isLoadMore: true,
      page: 1,
      total_page: 0,
      listData: []
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isLoadMore) {
      this.getMoreData();
    } else {
      console.log('没有更多了');
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  changeTap(e) {
    let ind = e.detail.index;
    this.setData({
      active: ind
    })
    this.init();
    this.getData();
  },
  getData() {
    let json = {
      amount_type:''
    }
    if(this.data.coupon){
      // 提货券明细
      if (this.data.active == 0) {
        json.amount_type = [1,2];
      } else if (this.data.active == 1) {
        json.amount_type = '2';
      } else if (this.data.active == 2) {
        json.amount_type = "1";
      }
    } else if(this.data.isvip){
      json.amount_type = '2';
    } else{
      // 采购款明细
      if (this.data.active == 0) {
        json.amount_type = [3,4,5];
      } else if (this.data.active == 1) {
        json.amount_type = '3';
      } else if (this.data.active == 2) {
        json.amount_type = [4,5];
      }
    }
    if(this.data.debtId){
      json.debtor_id = this.data.debtId;
    }
    let urlStr = app.getNetAddresss('plugin.debt-shop.frontend.debt-member.creditLog');
    app._postNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        let res = resdata.data
        if (res.result == 1) {
          this.data.isLoadMore = true;
          this.data.total_page = res.data.last_page;
          if (!this.data.total_page) {
            this.data.total_page = 0;
          }
          this.setData({
            listData: res.data.data
          })
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  getMoreData() {
    let urlStr = "";
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      this.data.page = this.data.page + 1;
      let json = {
        amount_type:'',
        page:this.data.page
      }
      if(this.data.coupon){
        // 提货券明细
        if (this.data.active == 0) {
          json.amount_type = [1,2];
        } else if (this.data.active == 1) {
          json.amount_type = '2';
        } else if (this.data.active == 2) {
          json.amount_type = "1";
        }
      } else if(this.data.isvip){
        json.amount_type = '2';
      } else{
        // 采购款明细
        if (this.data.active == 0) {
          json.amount_type = [3,4,5];
        } else if (this.data.active == 1) {
          json.amount_type = '3';
        } else if (this.data.active == 2) {
          json.amount_type = [4,5];
        }
      }
      if(this.data.debtId){
        json.debtor_id = this.data.debtId;
      }
      urlStr = app.getNetAddresss('plugin.debt-shop.frontend.debt-member.creditLog');
      app._postNetWork({
        url: urlStr,
        data: json,
        success: (resdata) => {
          let res = resdata.data
          if (res.result == 1) {
            this.data.isLoadMore = true;
            this.setData({
              listData: this.data.listData.concat(res.data.data)
            })
          } else {
            this.data.page = this.data.page - 1;
            this.isLoadMore = false
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },
})